'use strict';

const Controller = require('egg').Controller;

class BlogController extends Controller {
  async list() {
    let {ctx}=this
    let res=await ctx.service.blog.blog.list()
    console.log(res);
    if(res.length>0){
        ctx.body={
            code:0,
            mes:"获取数据成功",
        
        }
    }else{
        ctx.body={
            code:2,
            mes:"没有数据"
        }
    }
  }
  async addlist(){
      let {ctx}=this
      let {title,text,name,img}=ctx.query
      console.log(title,text,name,img);
      if(!title||!text||!name||!img){
          ctx.body={
              code:2,
              mes:"没有参数"
          }
          return
      }

      let res=await ctx.service.blog.blog.addlist(ctx.query)
      console.log(res);
      if(res.affectedRows==1){
          ctx.body={
              code:0,
              mes:"添加成功"
          }
      }else{
          ctx.body={
              code:2,
              mes:"添加失败"
          }
      }
  }

  async del(){
      let {ctx}=this
      let {id}=ctx.query
      let res=await ctx.service.blog.blog.del(id)
      console.log(res);
      if(res.affectedRows==1){
          ctx.body={
              code:0,
              mes:"删除成功"
          }
      }else{
          ctx.body={
              code:2,
              mes:"删除失败"
          }
      }
  }

  async updata(){
      let {ctx}=this
      console.log(ctx.query);
      let res=await ctx.service.blog.blog.updata(ctx.query)
      console.log(res);
      if(res.affectedRows==1){
          ctx.body={
              code:0,
              mes:"修改成功"
          }
      }else{
          ctx.body={
              code:2,
              mes:"修改失败"
          }
      }
  }
}

module.exports = BlogController;
